<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>调式页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body>

<form class="layui-form" action="">

    <div class="mainBox">
        <div class="main-container mr-5">
            <div class="layui-form-item" id="field_type1">
                <label class="layui-form-label required">字段类型</label>
                <div class="layui-input-block">
                    <div name="field_type" id="field_type" value="2"></div>
                </div>
            </div>

            <div class="layui-form-item" id="address">
                <label class="layui-form-label required">点位地址</label>
                <div class="layui-input-block">
                    <input type="text" name="address" value="" required lay-verify="required" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">执行方式</label>
                <div class="layui-input-block">
                    <div name="execution_method" id="execution_method" value="0"></div>
                </div>
            </div>

            <div class="layui-form-item" id="field_bit" style="display: none">
                <label class="layui-form-label">字节地址</label>
                <div class="layui-input-block">
                    <input type="text" name="field_bit_address" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" id="address_value" style="display: none">
                <label class="layui-form-label" id="label_address1">地址值</label>
                <div class="layui-input-block">
                    <input type="text" name="address_value" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" id="get_data" style="display: none">
                <label class="layui-form-label">获取到值</label>
                <div class="layui-input-block">
                    <label class="layui-form-label" id="label_address">获取到值</label>
                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit=""
                    lay-filter="save">
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-md">
                重置
            </button>
        </div>
    </div>

</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>

<script>
    //上级的设备id
    const device_id = <?php echo htmlspecialchars($device_id); ?>;
    const child_id = <?php echo htmlspecialchars($child_id); ?>;
    // 相关接口
    const INSERT_API = "/app/admin/iot-device-child/debugging?device_id="+device_id+"&id="+child_id;

    // 执行方式 execution_method
    layui.use(["jquery", "xmSelect"], function () {
        let value = layui.$("#execution_method").attr("value");
        let initValue = value ? value.split(",") : [];
        layui.xmSelect.render({
            el: "#execution_method",
            name: "execution_method",
            filterable: true,
            initValue: initValue,
            data: [{"value": "0", "name": "读取"}, {"value": "1", "name": "写入"}],
            value: "1",
            model: {"icon": "hidden", "label": {"type": "text"}},
            clickClose: true,
            radio: true,
            on: function (data) {
                //获取类型的id
                const type_id = data.change[0].value;
                let type_value = layui.$("#field_type").attr("value");
                if (type_id === '0') {
                    document.getElementById("address_value").style.display = "none";
                }else if (type_id === '1') {
                    document.getElementById("address_value").style.display = "block";
                }
                if (type_value==='0'){
                    document.getElementById("label_address1").innerText="bit值";
                }
                document.getElementById("get_data").style.display = "none";
            }
        })
    });

    // 字段 字段类型 field_type
    layui.use(["jquery", "xmSelect", "popup"], function() {
        layui.$.ajax({
            url: "/app/admin/dict/get/modbusDataType",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#field_type").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#field_type",
                    name: "field_type",
                    initValue: initValue,
                    filterable: true,
                    data: res.data,
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                    on:function (data) {
                        //获取数据类型选中的值
                        const type_name = data.change[0].name;
                        if (type_name==='bit'){
                            document.getElementById("field_bit").style.display = "block";
                            document.getElementById("label_address1").innerText="bit值";
                        }else{
                            document.getElementById("field_bit").style.display = "none";
                            document.getElementById("label_address1").innerText="地址值";
                        }
                        document.getElementById("get_data").style.display = "none";
                    }
                });
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });


    //提交事件
    layui.use(["form", "popup"], function () {
        // 字段验证允许为空
        layui.form.verify({
            phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
            email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
            url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
            number: [/(^$)|^\d+$/,'只能填写数字'],
            date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
            identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        });
        layui.form.on("submit(save)", function (data) {
            data.field.device_id = device_id;
            layui.$.ajax({
                url: INSERT_API,
                type: "POST",
                dateType: "json",
                data: data.field,
                success: function (res) {
                    let get_data = res.data;
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        let value = layui.$("#execution_method").attr("value");
                        if (value==='0'){
                            document.getElementById("get_data").style.display="block";
                            document.getElementById("label_address").innerText=get_data[0];
                        }
                        //parent.refreshTable();
                        //parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                }
            });
            return false;
        });
    });

</script>

</body>
</html>
